<?php
namespace App\Wen\Utils;

use App\Models\WxDomainAuth;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Redis;

class SiteUtils{
    public static function forget_config_data(){
        Cache::tags('configData')->flush();
    }

    public static function get_license_file($user_id, $domain = ''){
        if(_empty_($user_id)){
            return null;
        }

        if(_empty_($domain)){
            $model = WxDomainAuth::where('user_id', $user_id)->first();
        }else{
            $model = WxDomainAuth::where('domain_1', $domain)->orWhere('domain_2', $domain)
                ->orWhere('domain_3', $domain)->orWhere('domain_4', $domain)
                ->orWhere('domain_5', $domain)->orWhere('domain_6', $domain)
                ->orWhere('domain_7', $domain)->first();
        }

        if($model){

            $domain = '';

            if($model->domain_1){
                if(!_empty_($domain)){
                    $domain .= ',';
                }
                $domain .= $model->domain_1;
            }
            if($model->domain_2){
                if(!_empty_($domain)){
                    $domain .= ',';
                }
                $domain .= $model->domain_2;
            }
            if($model->domain_3){
                if(!_empty_($domain)){
                    $domain .= ',';
                }
                $domain .= $model->domain_3;
            }
            if($model->domain_4){
                if(!_empty_($domain)){
                    $domain .= ',';
                }
                $domain .= $model->domain_4;
            }
            if($model->domain_5){
                if(!_empty_($domain)){
                    $domain .= ',';
                }
                $domain .= $model->domain_5;
            }
            if($model->domain_6){
                if(!_empty_($domain)){
                    $domain .= ',';
                }
                $domain .= $model->domain_6;
            }
            if($model->domain_7){
                if(!_empty_($domain)){
                    $domain .= ',';
                }
                $domain .= $model->domain_7;
            }

            $res = Http::asForm()->post('https://swoole.minisns.cn', [
                'model' => $model->toArray(),
            ]);
            if($res->successful()){
                return $res->body();
            }
        }else{
            return null;
        }
    }

    public static function is_need_captcha($scene){
        global $__MINI_GLOBAL_IP__,$__MINI_GLOBAL_DEVICE__,$__MINI_GLOBAL_PLATFORM__;
        if($__MINI_GLOBAL_DEVICE__ == 'pc'){
            return false;
        }
        $key = 'captcha_scenes';
        if($__MINI_GLOBAL_DEVICE__ == 'h5'){
            $key = 'captcha_scenes_h5';
        }else if($__MINI_GLOBAL_DEVICE__ == 'app'){
            if($__MINI_GLOBAL_PLATFORM__ == 'ios'){
                $key = 'captcha_scenes_ios_app';
            }else{
                $key = 'captcha_scenes_android_app';
            }
        }
        $captcha_scenes = Settings::get($key, []);
        if(in_array($scene, $captcha_scenes)){
            if(!Cache::has('captcha:ip:'.$__MINI_GLOBAL_DEVICE__.':'.$__MINI_GLOBAL_PLATFORM__.':'.$__MINI_GLOBAL_IP__)){
                return true;
            }
            Cache::forget('captcha:ip:'.$__MINI_GLOBAL_DEVICE__.':'.$__MINI_GLOBAL_PLATFORM__.':'.$__MINI_GLOBAL_IP__);
        }
        return false;
    }

}
